/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.ruslan.kolobok.screen;

import org.ruslan.kolobok.Assets;
import org.ruslan.kolobok.Settings;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class MainMenuScreenOld implements Screen {
	private static final int VIEWPORT_WIDTH = Gdx.graphics.getWidth();
	private static final int VIEWPORT_HEIGHT = Gdx.graphics.getHeight();

	private Game game;

	// private OrthographicCamera guiCam;
	// private SpriteBatch batcher;
	// private ShapeRenderer shapeRenderer;
	Stage stage;

	// shapes
	private Rectangle soundBounds;
	private Rectangle playBounds;
	private Rectangle highscoresBounds;
	private Rectangle helpBounds;
	private Vector3 touchPoint;

	private GLCommon gl;

	public MainMenuScreenOld(Game game) {
		this.game = game;
		//
		// guiCam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
		// guiCam.position.set(VIEWPORT_WIDTH / 2, VIEWPORT_HEIGHT / 2, 0);
		// batcher = new SpriteBatch();
		// shapeRenderer = new ShapeRenderer();
		//
		// // shapes
		// soundBounds = Utils.createRectangle(Assets.soundOn);
		// soundBounds.setX(0);
		// soundBounds.setY(0);
		// playBounds = Utils.createRectangle(Assets.play);
		// playBounds.setX((VIEWPORT_WIDTH - playBounds.getWidth()) / 2);
		// float y = VIEWPORT_HEIGHT - playBounds.getHeight() - 10;
		// playBounds.setY(y);
		// highscoresBounds = Utils.createRectangle(Assets.highScores);
		// highscoresBounds.setX((VIEWPORT_WIDTH - highscoresBounds.getWidth())
		// / 2);
		// y -= highscoresBounds.getHeight() + 10;
		// highscoresBounds.setY(y);
		// helpBounds = Utils.createRectangle(Assets.help);
		// helpBounds.setX((VIEWPORT_WIDTH - helpBounds.getWidth()) / 2);
		// y -= helpBounds.getHeight() + 10;
		// helpBounds.setY(y);
		// touchPoint = new Vector3();
		//
		gl = Gdx.gl;
		gl.glClearColor(1, 1, 1, 1);

		// Skin skin = new Skin(Gdx.files.internal(Assets.UI_FILE));
		// Skin skin = new Skin(Gdx.files.internal("data/button/lala.json"));
		Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));

		stage = new Stage();
		stage.addActor(new MyActor());
		// Image splashImage = new Image(Assets.barrel);
		// stage.addActor(splashImage);
		NinePatch upPatch = new NinePatch(Assets.up, 19, 18, 35, 37);
		NinePatch downPatch = new NinePatch(Assets.down, 19, 18, 35, 37);
		// Drawable up = new TextureRegionDrawable(Assets.barrel);
		// Drawable down = new TextureRegionDrawable(Assets.soundOff);
		Drawable checked = new TextureRegionDrawable(Assets.soundOn);
		Drawable up = new NinePatchDrawable(upPatch);
		Drawable down = new NinePatchDrawable(downPatch);
		// TextButtonStyle style = new TextButtonStyle(up, down, null);
		// style.font = new BitmapFont(false);
		// style.fontColor = new Color(1, 0, 0, 1);
		// Button button = new
		// TextButton("lalala sdsdf dsfd fs sd s f sd ds sf fds", style);
		final Button button1 = new TextButton("lalala1", skin);
		button1.addListener(new InputListener() {
	        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
	                System.out.println("down");
	                return true;
	        }
	        
	        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
	                System.out.println("up: (" + x + ", " + y + ")");
	                MoveToAction action = new MoveToAction();
	                action.setPosition(button1.getX() + 100, button1.getY() + 100);
	                action.setDuration(2);
	                button1.addAction(action);
	        }
	});
		
		Button button2 = new TextButton("lalala2", skin);
		Button button3 = new TextButton("lalala3", skin);
		// Drawable imageUp = new TextureRegionDrawable(Assets.play);
		// Button button = new ImageButton(imageUp);

		// creates the table actor
		Table table = new Table(skin);
		// 100% width and 100% height on the table (fills the stage)
		table.setFillParent(true);
		// add the table to the stage
		stage.addActor(table);
		// add the welcome message with a margin-bottom of 50 units
		table.add("Welcome to Tyrian for Android!").spaceBottom(50);
		// move to the next row
		table.row();
		// add the start-game button sized 300x60 with a margin-bottom of 10
		// units
		table.add(button1).size(300f, 60f).uniform().spaceBottom(10);
		// move to the next row
		table.row();
		// add the options button in a cell similiar to the start-game button's
		// cell
		table.add(button2).uniform().fill().spaceBottom(10);
		// move to the next row
		table.row();
		// add the high-scores button in a cell similiar to the start-game
		// button's cell
		table.add(button3).uniform().fill();

		stage.addActor(table);
		Gdx.input.setInputProcessor(stage);
	}

	// public boolean update(float deltaTime) {
	// // touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0);
	// // guiCam.unproject(touchPoint);
	// if (Gdx.input.justTouched()) {
	// touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0);
	// guiCam.unproject(touchPoint);
	//
	// if (OverlapTester.pointInRectangle(playBounds, touchPoint.x,
	// touchPoint.y)) {
	// Assets.playSound(Assets.clickSound);
	// game.setScreen(new GameScreen(game));
	// return false;
	// }
	// if (OverlapTester.pointInRectangle(highscoresBounds, touchPoint.x,
	// touchPoint.y)) {
	// Assets.playSound(Assets.clickSound);
	// game.setScreen(new HighscoresScreen(game));
	// return false;
	// }
	// if (OverlapTester.pointInRectangle(helpBounds, touchPoint.x,
	// touchPoint.y)) {
	// Assets.playSound(Assets.clickSound);
	// game.setScreen(new HelpScreen(game));
	// return false;
	// }
	// if (OverlapTester.pointInRectangle(soundBounds, touchPoint.x,
	// touchPoint.y)) {
	// Assets.playSound(Assets.clickSound);
	// Settings.soundEnabled = !Settings.soundEnabled;
	// if (Settings.soundEnabled)
	// Assets.music.play();
	// else
	// Assets.music.pause();
	// }
	// }
	//
	// return true;
	// }

	// public void draw(float deltaTime) {
	// gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	// guiCam.update();
	// batcher.setProjectionMatrix(guiCam.combined);
	//
	// batcher.disableBlending();
	// batcher.begin();
	// batcher.draw(Assets.backgroundRegion, 0, 0, VIEWPORT_WIDTH,
	// VIEWPORT_HEIGHT);
	// batcher.end();
	//
	// batcher.enableBlending();
	// batcher.begin();
	// Utils.drawTextureRegion(batcher, Assets.play, playBounds);
	// Utils.drawTextureRegion(batcher, Assets.highScores, highscoresBounds);
	// Utils.drawTextureRegion(batcher, Assets.help, helpBounds);
	// Utils.drawTextureRegion(batcher, Settings.soundEnabled ? Assets.soundOn :
	// Assets.soundOff, soundBounds);
	// batcher.end();
	//
	// drawAux();
	// }

	// private void drawAux() {
	// // draw AUX
	// shapeRenderer.setProjectionMatrix(guiCam.combined);
	// shapeRenderer.begin(ShapeType.Rectangle);
	// Utils.drawRect(shapeRenderer, playBounds, Color.RED);
	// Utils.drawRect(shapeRenderer, highscoresBounds, Color.GREEN);
	// Utils.drawRect(shapeRenderer, helpBounds, Color.BLUE);
	// Utils.drawRect(shapeRenderer, soundBounds, Color.BLACK);
	// shapeRenderer.end();
	//
	// shapeRenderer.begin(ShapeType.FilledCircle);
	// shapeRenderer.setColor(Color.CYAN);
	// shapeRenderer.filledCircle(touchPoint.x, touchPoint.y, 10);
	// shapeRenderer.end();
	// }

	@Override
	public void render(float delta) {
		// boolean draw = update(delta);
		// if (draw) {
		// draw(delta);
		// }

		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(width, height, true);
	}

	@Override
	public void show() {
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
		Settings.save();
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		stage.dispose();
	}

	// private ShapeRenderer renderer;
	//
	// public void draw (SpriteBatch batch, float parentAlpha) {
	// batch.end();
	//
	// renderer.setProjectionMatrix(batch.getProjectionMatrix());
	// renderer.setTransformMatrix(batch.getTransformMatrix());
	// renderer.translate(getX(), getY(), 0);
	//
	// renderer.begin(ShapeType.Rectangle);
	// renderer.rect(0, 0, getWidth(), getHeight());
	// renderer.end();
	//
	// batch.begin();
	// }

	public class MyActor extends Actor {
		TextureRegion region;

		public MyActor() {
			region = new TextureRegion(Assets.backgroundRegion);
		}

		public void draw(SpriteBatch batch, float parentAlpha) {
			Color color = getColor();
			batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
			batch.draw(region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(),
					getScaleY(), getRotation());
		}
	}
}
